﻿using System;
using System.Collections.Generic;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;

namespace LINQPad
{
	public static class Activator
	{
		public static void Do()
		{
			Activator.WriteLicense();
		}

		public static byte[] GetEncryptedLicense()
		{
			byte[] rawLicense = Activator.GetRawLicense();
			byte[] array;
			using (RSACryptoServiceProvider rsacryptoServiceProvider = new RSACryptoServiceProvider())
			{
				rsacryptoServiceProvider.ImportCspBlob(Convert.FromBase64String(Activator.PrivateKey));
				array = rsacryptoServiceProvider.SignData(rawLicense, SHA1.Create());
			}
			List<byte> list = new List<byte>
			{
				6,
				1
			};
			list.AddRange(BitConverter.GetBytes(Convert.ToInt16(rawLicense.Length)));
			list.AddRange(BitConverter.GetBytes(Convert.ToInt16(array.Length)));
			list.AddRange(rawLicense);
			list.AddRange(array);
			return list.ToArray();
		}

		public static IsolatedStorageFile GetIsolatedStorageFile(bool forAllUsers)
		{
			string text = "Resources";
			string text2 = "Class157";
			string methodName = "smethod_18";
			Assembly assembly = Assembly.Load(text);
			if (assembly == null)
			{
				throw new Exception("Activator. Null when loading " + text);
			}
			Assembly assembly2 = assembly;
			Type type = assembly2.GetType(text2);
			if (type == null)
			{
				throw new Exception("Activator. Null when loading type " + text2);
			}
			Type type2 = type;
			MethodInfo methodInfo = type2.GetMethods(BindingFlags.Static | BindingFlags.NonPublic).FirstOrDefault((MethodInfo x) => x.Name == methodName);
			if (methodInfo == null)
			{
				throw new Exception("Activator. Null when loading method " + methodName);
			}
			MethodInfo methodInfo2 = methodInfo;
			IsolatedStorageFile isolatedStorageFile = (IsolatedStorageFile)methodInfo2.Invoke(null, new object[]
			{
				forAllUsers
			});
			IsolatedStorageFile isolatedStorageFile2 = isolatedStorageFile;
			if (isolatedStorageFile2 == null)
			{
				throw new Exception("Activator. Null when opening IsolatedStorageFile");
			}
			return isolatedStorageFile2;
		}

		private static byte[] GetRawLicense()
		{
			byte[] array = MD5.Create().ComputeHash(Encoding.UTF8.GetBytes(Environment.MachineName));
			byte[] result;
			using (MemoryStream memoryStream = new MemoryStream())
			{
				using (Aes aes = Aes.Create())
				{
					using (ICryptoTransform cryptoTransform = aes.CreateEncryptor(array, array))
					{
						using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
						{
							using (BinaryWriter binaryWriter = new BinaryWriter(cryptoStream))
							{
								binaryWriter.Write(Environment.UserName);
								binaryWriter.Write(".");
								binaryWriter.Write(-1);
								binaryWriter.Write(-1);
								binaryWriter.Write("Document");
								binaryWriter.Write("HeaderText");
								binaryWriter.Write("FooterText");
								binaryWriter.Write(Guid.Empty.ToByteArray());
								binaryWriter.Write(DateTime.Now.AddYears(90).Ticks);
								binaryWriter.Write(2);
								binaryWriter.Write(-1);
								binaryWriter.Write("V5V6");
								binaryWriter.Write("PREM");
								binaryWriter.Flush();
								binaryWriter.Close();
								result = memoryStream.ToArray();
							}
						}
					}
				}
			}
			return result;
		}

		private static void ReadExistLicense()
		{
			string path = "linqpad_isolated_storage_file_content";
			byte[] array = null;
			try
			{
				using (IsolatedStorageFile isolatedStorageFile = Activator.GetIsolatedStorageFile(false))
				{
					bool flag = isolatedStorageFile.GetFileNames("LastWindowPosition").Length != 0;
					if (flag)
					{
						using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("LastWindowPosition", FileMode.Open, isolatedStorageFile))
						{
							using (BinaryReader binaryReader = new BinaryReader(isolatedStorageFileStream))
							{
								array = binaryReader.ReadBytes(10000);
							}
						}
					}
				}
				bool flag2 = array != null;
				if (flag2)
				{
					File.WriteAllBytes(path, array);
				}
			}
			catch (Exception ex)
			{
				File.WriteAllText(path, ex.ToString());
			}
		}

		private static void WriteLicense()
		{
			string path = "linqpad_isolated_storage_file_write_error";
			byte[] encryptedLicense = Activator.GetEncryptedLicense();
			try
			{
				using (IsolatedStorageFile isolatedStorageFile = Activator.GetIsolatedStorageFile(false))
				{
					using (IsolatedStorageFileStream isolatedStorageFileStream = new IsolatedStorageFileStream("LastWindowPosition", FileMode.Create, isolatedStorageFile))
					{
						isolatedStorageFileStream.Write(encryptedLicense, 0, encryptedLicense.Length);
					}
				}
			}
			catch (Exception ex)
			{
				File.WriteAllText(path, ex.ToString());
			}
		}

		public static string PrivateKey = "BwIAAACkAABSU0EyAAQAAAEAAQBFtstFzq2GgWryQbNhAZqCv7RQAvmQx6TEri2WPjEn5iTMOd8o1gM4a+ba7ZJ9+Hk3FIM82l4St5JfdMRKTfg//pd1f+DowAIZq290+p07YMcVfEJoIbKixYJyGn3ouuk0zhflBA64NfIF5hR3lNTJUP67DNfV6SB9V6TJdzdUtxWg8u2yzJLy0PSOxV8LLeTRKB+S2e8K5217fpcCoJSxl9nSA1CmSqdFEfUpROuWR62zJde9VfmPCG3QnXivY99xGQ1/8ofBZj/0+/jXhoEzMSe34VBfrnxM/n9G0acg62cDTP3bls4GT/E8lNVQqEnXNxULNceZ3xbbbamFbRfSXVR9eZLyGFGKBXgUpxkVgmhjLNQM8nMmviykUmjQGOD+Na0OGSj6ytwZnelszJeWMXQUUK7kOHrFjVL3j3H0wuFZnCwsd1w/LzEHC3wSqEhzQR62FGRAX/FarhgK7PMDekCfBal32Q42zzHkr32CyS84dbc7LqjiwAMgukUN76lzoUOdKlQqLBxINfDf/CgylzYDd7VoMpEj+JGpaK71/QKuI8uwcO9RXzr4bhJI5BKAKztHK+6zwdtK4oe/tWdYkeGKnhodr3JXAqLPDB14lqLI/pNeQAHeb86RdjvqF6Hl2+mJj2KGh9N2NKBrDLG9GotbP+f3grrjWnm+dYjS9s3yRfdaPqS8/sIINsrdWjcqGuarxM84R8bmKNSnSVyRK/DUFIGyeLuSxpOR1a4I6pMR1zK//joATgKQ3y7Uqyk=";
	}
}
